pipeline {
    agent any
    stages {
<#if mode != "multistage">
        stage("build") {
            steps {
                sh 'echo "build" > Jenkins_result'
                sh 'npm i --unsafe-perm --registry=https://registry.npm.taobao.org'
                sh 'npm run ${profile}'
                sh 'echo "" > Jenkins_result'
            }
        }
</#if>
<#if mode == "jib">
        stage("jib") {
            steps {
                withCredentials([usernamePassword(credentialsId: "${repoCredential}", usernameVariable: "DOCKER_REPO_USERNAME", passwordVariable: "DOCKER_REPO_PASSWORD")]) {
                    sh 'echo "build" > Jenkins_result'
                    sh '''${tagCommand}'''
                    sh '''
                    jib_build_cmd="jib build \
                    --allow-insecure-registries \
                    --to-username $DOCKER_REPO_USERNAME \
                    --to-password $DOCKER_REPO_PASSWORD "
                    for tag in `cat .tags`; do
                        eval "${r'${jib_build_cmd}'} --target ${imageName}:${r'${tag}'}"
                    done
                    '''
                    sh 'echo "" > Jenkins_result'
                }
            }
        }
<#else >
        stage("docker") {
            steps {
                withCredentials([usernamePassword(credentialsId: "${repoCredential}", usernameVariable: "DOCKER_REPO_USERNAME", passwordVariable: "DOCKER_REPO_PASSWORD")]) {
                    sh 'echo "build" > Jenkins_result'
                    sh '''${tagCommand}'''
                    sh 'docker login ${dockerRepoAddress} -u ${r'$DOCKER_REPO_USERNAME'} -p ${r'$DOCKER_REPO_PASSWORD'}'
                    sh '''
                    image_tag=""
                    for tag in `cat .tags`; do
                        image_tag=${r'${image_tag}'}"-t ${imageName}:${r'${tag}'} "
                    done
                    eval "docker build ${r'${image_tag}'} ."
                    '''
                    sh '''
                    for tag in `cat .tags`; do
                        docker push ${imageName}:${r'${tag}'}
                    done
                    '''
                    sh 'docker logout ${dockerRepoAddress}'
                    sh 'echo "" > Jenkins_result'
                }
            }
        }
</#if>
        stage("deploy") {
            steps {
                sh 'echo "deploy" > Jenkins_result'
                sh '''k8s-cmd deploy \
                --kubeconfig ${kubeconfig} \
                -n ${namespace} \
                -a ${deploymentName} \
                -i ${allImageName} \
                -T `cat .tag`<#if k8sYamlDir?exists> --k8s-yaml-dir ${k8sYamlDir}</#if>
                '''
                sh 'echo "" > Jenkins_result'
            }
        }
        stage("check") {
            steps {
                sh 'echo "check" > Jenkins_result'
                sh '''k8s-cmd check \
                --kubeconfig ${kubeconfig} \
                -n ${namespace} \
                -a ${deploymentName} \
                -i ${allImageName} \
                -T `cat .tag`
                '''
                sh 'echo "" > Jenkins_result'
            }
        }
    }
    post {
        success {
            // Usage remind-v2.py {cluster} 1 {msg}
            sh 'remind-v2.py ${cluster} 1 ${msg}'
        }
        failure {
            // Usage remind-v2.py {cluster} 0 {msg}
            sh 'remind-v2.py ${cluster} 0 ${msg}'
        }
    }
}
